﻿using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Dto.Customer;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Web.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CRM.Admin.Areas.Customer.Controllers
{
    public class CustomerProfilesController : Controller
    {
        public ICustomerProfilesService CustomerProfilesService { get; set; }
        // GET: Customer/CustomerProfiles
        public ActionResult CustomerProfilesAdd(Guid CustomerId)
        {
            ViewBag.CustomerId = CustomerId;
            return View();
        }
        public ActionResult CustomerProfilesEdit(Guid Id)
        {
            var entity = CustomerProfilesService.Item.FirstOrDefault(a=>a.Id==Id).MapTo<CustomerProfilesDto>();
            return View(entity);
        }
        public ActionResult CustomerProfilesInsert(CustomerProfilesDto dto)
        {
            dto.CheckNotNull(nameof(dto));

            var result = CustomerProfilesService.Insert(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "顾客简介添加成功";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult CustomerProfilesUpdate(CustomerProfilesDto dto)
        {
            dto.CheckNotNull(nameof(dto));

            var result = CustomerProfilesService.Update(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "顾客简介修改成功";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult CustomerProfilesDelte(Guid id)
        {
            id.CheckNotEmpty("id");
            var result = CustomerProfilesService.Delete(id);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "顾客简介删除成功";
            }
            return Json(new AjaxResult(msg, msgType));
        }
    }
}